
----------------------------------------------------------------
--- Copyright：  迷你玩-海马
--- Description：数据缓存服务器
----------------------------------------------------------------
--- 1 从指定的 ssdb 中读取数据 scan模式
--- 2 写入到指定mysql dc中去
-------------------------------------------------------------------



local assert     = assert
local require    = require
local bit        = bit
local coroutine  = coroutine
local debug      = debug
local io         = io
local pairs      = pairs
local ipairs     = ipairs
local math       = math
local os         = os
local print      = print
local pcall      = pcall
local xpcall     = xpcall
local rawget     = rawget
local rawset     = rawset
local select     = select
local string     = string
local table      = table
local tonumber   = tonumber
local tostring	 = tostring
local error      = error
local type       = type
local unpack     = unpack
local setmetatable = setmetatable
local getmetatable = getmetatable
local ngx = ngx

local string_char  = string.char
local string_sub   = string.sub
local string_byte  = string.byte
local string_gsub  = string.gsub
local string_find  = string.find
local string_upper = string.upper

local math_fmod   = math.fmod
local math_floor  = math.floor
local math_pow    = math.pow
local math_abs    = math.abs


local uu           = require( "modules.commhm.utils" )
local log          = require( "modules.commhm.log" )
local ns_time      = require( "modules.commhm.time" )
local ns_network   = require( "modules.commhm.network" )
local ns_http      = require( "modules.commhm.resty.http" )

local ns_const_vars  = require("modules.commhm.const_vars")

local ns_config       = require( "modules.data_cache.scan_sync_config" )

local gg              = require( "modules.data_cache.data_cache_gg" )
local ns_auth_checker = require( "modules.data_cache.auth_checker" )

local ns_share_dict_checker = require("modules.commhm.framework.share_dict_checker");

local ns_ssdbDcClient  = require("modules.commhm.dc.ssdbDcClient");
local ns_mysqlDcClient = require("modules.commhm.dc.mysqlDcClient");

--local ns_luaConfigMgr  = require("modules.center_server.luaConfigProxy")
local ns_centerServerProxy   = require("modules.commhm.centerServerProxy");


local FOO = {
	_VERSION = '0.12.1',
	cmds = {},

	worker_id = ngx.worker.id(),

	sync_ssdb_dc_client   = nil,   -- from
	sync_mysql_dc_client  = nil,   -- to

	target_select_by_key_mod = nil,       -- 是否需要重新计算select值

	ma_gate_load             = {},  --网关的负载值描述
};



------------初始化
FOO.init = function()

	-- 1 init ssdb dc 连接管理 (from)
	if  (not FOO.sync_ssdb_dc_client) and ns_config.doing.sync_ssdb_server_dc then
		log.debug( "create new ns_ssdbDcClient" );
		FOO.sync_ssdb_dc_client = ns_ssdbDcClient:new( "sync_dc_ssdb",
				ns_config.doing.sync_ssdb_server_dc.server,
				ns_config.doing.sync_ssdb_server_dc.select );
	end


	-- 2 init mysql dc2 连接管理 (to)
	if  (not FOO.sync_mysql_dc_client) and ns_config.doing.sync_mysql_server_dc2 then
		log.debug( "create new ns_mysqlDcClient" );
		FOO.sync_mysql_dc_client = ns_mysqlDcClient:new( "sync_dc_mysql",
				ns_config.doing.sync_mysql_server_dc2.server,
				ns_config.doing.sync_mysql_server_dc2.select )
	end

	-- 是否需要重新计算select值 ( from 1-ssdb to 10-mysql )
	if  ns_config.doing.target_select_by_key_mod then
		FOO.target_select_by_key_mod = ns_config.doing.target_select_by_key_mod
	end

end



FOO.final = function()
	if  ngx.ctx.gg_final_has_called then
		-- 防止无限循环 只调一次
	else
		ngx.ctx.gg_final_has_called = true;

		if  FOO.sync_ssdb_dc_client then
			FOO.sync_ssdb_dc_client:final();
		end

		if  FOO.sync_mysql_dc_client then
			FOO.sync_mysql_dc_client:final();
		end

	end

end


---------------------
--- 获得整体系统ma_gate的负载值
--- !!! 必须在FOO中增加：  ma_gate_load            = {},  --网关的负载值描述
function FOO.getMaGateLoad( server_name_ )
	if uu.check_worker_time_interval( "MaGateLoad", 71 ) then
		local load_info_ =  ns_centerServerProxy.WWW_getLoadByName( server_name_, "ma_gate" )
		--uu.var_dump( load_info_ )
		if  load_info_ and load_info_.ma_gate and load_info_.ma_gate.load_index then
			FOO.ma_gate_load.load_index = load_info_.ma_gate.load_index
		end
	end
	return FOO.ma_gate_load.load_index or 0
end


return FOO;
